package com.fuershiye.woman.monitor.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.fuershiye.woman.monitor.model.Content;

@Repository("contentDao")
@SuppressWarnings("unchecked")
public class ContentDao {

    @Autowired
    private HibernateDao hibernateDao;

    public void save(Content content){
        hibernateDao.save(content);
    }

    public Content get(Long id) {
        return hibernateDao.getById(Content.class,id);
    }

    public void delete(Long id) {
        String sql = "update Content set status = 0 where id = ?";
        SQLQuery query = hibernateDao.getSqlQuery(sql).addEntity(Content.class);
        query.setLong(0, id);
        query.executeUpdate();
    }
    
    @SuppressWarnings("unchecked")
	public List<Content> getPage(Integer type, Integer page, Integer pageSize) {

    	StringBuffer sb = new StringBuffer("from Content where 1=1 ");
    	
    	if(type!=null && type!=0){
    		sb.append("and type =:type ");
    	}
		if(page==null||page==0){
			page= 1;
		}
		if(pageSize==null||pageSize==0){
			pageSize= 30;
		}
		sb.append("ORDER BY id DESC");
    	Query query = hibernateDao.getQuery(sb.toString());
    	
    	if(type!=null && type!=0){
            query.setInteger("type",type);
    	}
    	//size 		10
    	//page 		1,	 2,		3
    	//return 	0-9, 10-19,	20-29
    	query.setFirstResult((page-1)*pageSize);
    	query.setMaxResults(page*pageSize-1);
    	
        return query.list();
    }

}
